package com.cskaoyan.controller.backStage;

import com.cskaoyan.model.Issue;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.model.vo.IssueDataVO;
import com.cskaoyan.service.IssueService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: IssueController
 * @Description: TODO 通用问题模块
 * @Author:zk
 * @Date: 2022/5/5 20:26
 * @Version: 1.0
 **/
@RestController
@RequestMapping("admin/issue")
public class IssueController {
    @Autowired
    IssueService issueService;

    /**
     * @author: zk
     * @description: TODO 查询通用问题列表
     * @date: 2022/5/5 21:25
     * @return BaseRespVO<IssueDataVO>
     */
    @RequiresPermissions("admin:issue:list")
    @RequestMapping("list")
    public BaseRespVO issueList(String question, BaseParamBO baseParamBO){
        IssueDataVO issueDataVO = issueService.query(question,baseParamBO);
        BaseRespVO<IssueDataVO> issueRespVO = new BaseRespVO<>();
        issueRespVO.setErrmsg("成功");
        issueRespVO.setData(issueDataVO);
        return issueRespVO;
    }

    /**
     * @author: zhoukang
     * @description: TODO 新增问题
     * @date: 2022/5/5 23:06
     * @Param:question ,answer
     * @return BaseRespVO(data:Issue)
     */
    @RequiresPermissions("admin:issue:create")
    @RequestMapping("create")
    public BaseRespVO issueCreate(@RequestBody Map issueReq){
        //输入情况判断
        String question =(String) issueReq.get("question");
        String answer = (String)issueReq.get("answer");
        BaseRespVO<Issue> issueRespVO = new BaseRespVO<>();
        if (question.trim().length()==0||answer.trim().length()==0){
            issueRespVO.setErrmsg("输入不可为空字符串");
            issueRespVO.setErrno(402);
            return issueRespVO;
        }
        if (question.length()>50){
            issueRespVO.setErrmsg("问题描述超过字数限制(50)");
            issueRespVO.setErrno(402);
            return issueRespVO;
        }
        if (answer.length()>300){
            issueRespVO.setErrmsg("问题回复答案超过字数限制(300)");
            issueRespVO.setErrno(402);
            return issueRespVO;
        }
        Issue issue = issueService.insert(question,answer);
        issueRespVO.setErrmsg("成功");
        issueRespVO.setData(issue);
        return issueRespVO;
    }

    /**
     * @author: zhoukang
     * @description: TODO 逻辑删除通用问题
     * @date: 2022/5/6 11:38
     * @Param: issue
     * @return ExceptionVO
     */
    @RequiresPermissions("admin:issue:delete")
    @RequestMapping("delete")
    public ExceptionVO issueDelete(@RequestBody  Issue issue){
        issueService.delete(issue);
        return new ExceptionVO();
    }
    @RequiresPermissions("admin:issue:update")
    @RequestMapping("update")
    public BaseRespVO issueUpdate(@RequestBody Issue issue){
        Issue issueData = issueService.update(issue);
        return BaseRespVO.ok(issueData);
    }

}
